package mnn;

/*
 This program is written by Ahmed Medhat Ahmed
 Egypt
 Alexandria Universty
 Faculty of Science
 Computer Science
 7-7-2007
 a.medhat.cs@gmail.com
 ama_compsc@yahoo.com
 */

import java.util.Random;

public class TestNN {

	private int input;
	private int output;
	private int hiddenlayer;
	private int hiddenneurons;
	private double learnrate;
	private double momentumrate;
	private Random rand;

	private double out_arr[];

	private String outstring;

	private ANN neuro;

	public TestNN() {

	}

	public TestNN(int in, int out, int hl, int hn, double lr, double mr) {
		input = in;
		output = out;
		hiddenlayer = hl;
		hiddenneurons = hn;
		learnrate = lr;
		momentumrate = mr;

		rand = new Random(0);

		neuro = new ANN(input, output, hiddenlayer, hiddenneurons, learnrate,
				momentumrate, rand);

		out_arr = new double[out];
	}

	public void getDim4(int[] Dim4) {
		neuro.func2(Dim4);
	}

	public void getweightsDim(int[] Dim) {
		int con[] = new int[1];// connectionlayers
		int nlayer[] = new int[1];// neuronlayers
		int maxneur[] = new int[1];// maxneurons

		neuro.func1(con, nlayer, maxneur);

		Dim[0] = con[0];
		Dim[1] = nlayer[0];
		Dim[2] = maxneur[0];
	}

	public void setwaites(double[][][] weight) {
		neuro.setwaites(weight);
	}

	public String start(int[] in_array, int[][] out_array) {
		double error = 0.0;
		double errorsum = 0.0;
		double avg = 0.0;
		double minerror = 100.0;
		int c = 0;
		double Din_array[] = new double[input];
		double Dout_array[][] = new double[output][output];
		// converting from integer to double

		for (int k = 0; k < input; k++)
			Din_array[k] = Double.valueOf(in_array[k]);
		for (int i = 0; i < output; i++)
			for (int j = 0; j < output; j++)
				Dout_array[i][j] = Double.valueOf((out_array[i][j]));// .toString());

		outstring = "";
		out_arr = neuro.getResult(Din_array);
		for (int i = 0; i < output; i++) {
			for (int j = 0; j < output; j++) {
				error = (Dout_array[i][j] - out_arr[j]) == 0.0 ? 0.0 : 1.0;
				errorsum += error;
			}
			avg = errorsum / output;
			outstring += "\n\nAverage error with testing class number  "
					+ (i + 1) + " is " + (avg);
			if (errorsum < minerror) {
				minerror = errorsum;
				c = i;
			}
		}
		outstring += "\n\n The testing image belongs to class number "
				+ (c + 1);
		return outstring;
	}
}